﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Business;

public partial class client_Client  : @base{
        
    protected void Page_Load(object sender, EventArgs e) {
        if (UserDetails == null)
        {
            string jscript = "<script language='javascript'  type='text/javascript'> parent.document.location.href= '../Default.aspx'</script>";
            //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key3ClientBlock", jscript);

        }
        if (!IsPostBack)
        {
            
            
            if (UserDetails.Is_admin == "Admin")
                Response.Redirect("../admin/Home.aspx");
            else
            {
                report_content.Attributes["src"] = "Welcome.aspx";
                int i = (int)Session["treeview"];
                if (i == 1)
                    Generate_TreeView();
            }
        }
        
    }

    protected void Generate_TreeView() {
        try {
            CreateConn objcon = new CreateConn();
            string client_query = "select clientName, c.client_id AS client from Clients c, Users u"
            + " where u.client_id = c.client_id And u.username = '"
            + Session["username"].ToString() + "'";
            DataTable dt = objcon.SQLConnDataTable(client_query);
            TreeNode node;

            // Add the Root Client
            node = new TreeNode(dt.Rows[0]["clientName"].ToString());
            node.SelectAction = TreeNodeSelectAction.SelectExpand;
            node.NavigateUrl = "Welcome.aspx";
            node.Target = "report_content";

            tree1.Nodes.Add(node);

            // find the subclient
            objcon = new CreateConn();
            string query1 = "select clientName from Clients c where c.parent_client_id = " + dt.Rows[0]["client"];
            DataTable dt1 = objcon.SQLConnDataTable(query1);

            if (dt1.Rows.Count > 0) {

                // Add the subclient in main client
                TreeNode man = new TreeNode();
                for (int j = 0; j < tree1.Nodes.Count; j++) {
                    man = tree1.Nodes[j];

                    foreach (DataRow dr in dt1.Rows) {
                        TreeNode node1 = new TreeNode(dr["clientName"].ToString());
                        node1.SelectAction = TreeNodeSelectAction.SelectExpand;
                        node1.NavigateUrl = "#";
                        man.ChildNodes.Add(node1);
                    }
                }

                TreeNode thirdnode = new TreeNode();
                for (int k = 0; k < man.ChildNodes.Count; k++) {
                    thirdnode = man.ChildNodes[k];
                    objcon = new CreateConn();
                    string query_site = "select siteName from Sites s, Clients c where c.client_id = s.client_id and c.clientName='" + thirdnode.Text + "'";
                    DataTable dt_sites = objcon.SQLConnDataTable(query_site);

                    foreach (DataRow dr in dt_sites.Rows) {
                        TreeNode node1 = new TreeNode(dr["siteName"].ToString());
                        node1.SelectAction = TreeNodeSelectAction.SelectExpand;
                        node1.NavigateUrl = "#";
                        thirdnode.ChildNodes.Add(node1);
                        thirdnode.CollapseAll();
                    }

                    for (int l = 0; l < thirdnode.ChildNodes.Count; l++) {
                        TreeNode fourthnode = thirdnode.ChildNodes[l];
                        String siteName = fourthnode.Text;
                        objcon = new CreateConn();
                        string query2 = "select machineName, machine_id, client_id from Machines m, Sites s where s.site_id = m.site_id AND m.status = 'Activated' and s.siteName='" + fourthnode.Text + "'";
                        DataTable dt2 = objcon.SQLConnDataTable(query2);

                        foreach (DataRow dr in dt2.Rows) {
                            String machinename = dr["machineName"].ToString();
                            String machineid = dr["machine_id"].ToString();
                            String client_id = dr["client_id"].ToString();
                            TreeNode node1 = new TreeNode(dr["machineName"].ToString());
                            node1.SelectAction = TreeNodeSelectAction.SelectExpand;

                            node1.NavigateUrl = "Vibration_Report.aspx?id=" + machineid + "&client_id =" + client_id+"&machineName="+machinename;
                            node1.Target = "report_content";
                            node1.ImageUrl = GetIcon(machineid);
                            fourthnode.ChildNodes.Add(node1);
                            fourthnode.CollapseAll();
                        }
                    }
                }
            } else {
                TreeNode thirdnode = new TreeNode();
                for (int k = 0; k < tree1.Nodes.Count; k++) {
                    thirdnode = tree1.Nodes[k];
                    objcon = new CreateConn();
                    string query_site = "select siteName from Sites s, Clients c where c.client_id = s.client_id and c.clientName='" + thirdnode.Text + "'";
                    DataTable dt_sites = objcon.SQLConnDataTable(query_site);

                    foreach (DataRow dr in dt_sites.Rows) {
                        TreeNode node1 = new TreeNode(dr["siteName"].ToString());
                        node1.SelectAction = TreeNodeSelectAction.SelectExpand;
                        node1.NavigateUrl = "#";
                        thirdnode.ChildNodes.Add(node1);
                        thirdnode.CollapseAll();
                    }

                    for (int l = 0; l < thirdnode.ChildNodes.Count; l++) {
                        TreeNode fourthnode = thirdnode.ChildNodes[l];
                        String siteName = fourthnode.Text;
                        objcon = new CreateConn();
                        string query2 = "select machineName, machine_id, client_id from Machines m, Sites s where s.site_id = m.site_id AND m.status = 'Activated' and s.siteName='" + fourthnode.Text + "'";
                        DataTable dt2 = objcon.SQLConnDataTable(query2);

                        foreach (DataRow dr in dt2.Rows) {
                            String machinename = dr["machineName"].ToString();
                            String machineid = dr["machine_id"].ToString();
                            String client_id = dr["client_id"].ToString();
                            TreeNode node1 = new TreeNode(dr["machineName"].ToString());
                            node1.SelectAction = TreeNodeSelectAction.SelectExpand;
                            node1.NavigateUrl = "Vibration_Report.aspx?id=" + machineid + "&client_id=" + client_id + "&machineName=" + machinename;
                            node1.ImageUrl = GetIcon(machineid);
                            node1.Target = "report_content";
                            fourthnode.ChildNodes.Add(node1);
                            fourthnode.CollapseAll();
                        }
                    }
                }
            }
        } catch (Exception err) {
            Console.Write("Err msg:" + err.Message);
        }
    }

    protected string StatusToIcon(string statusID) {
        string src = "";

        if (statusID.Equals("4"))
        {
            src = "../images/machine_red.png";
            return src;
        }
        else if (statusID.Equals("3"))
        {
            src = "../images/machine_orange.png";
            return src;
        }
        else if (statusID.Equals("2"))
        {
            src = "../images/machine_yellow.png";
            return src;
        }
        else if (statusID.Equals("1"))
        {
            src = "../images/machine_green.png";
            return src;
        }
        else
        {
            src = "../images/machine_blue.png";
        }
    
        return src;
    }

    protected string GetIcon(string machineID) {
       
        string src = String.Empty;
        string query = "SELECT   MAX(status_id) AS status_id"
+ " FROM      ComponentStatus"
+ " WHERE   (component_id IN"
                    + " (SELECT   component_id"
                    + " FROM      Components"
                    + " WHERE   (machine_id = '" + machineID + "'))) AND (dateRecorded IN"
                    + " (SELECT   MAX(dateRecorded) AS dateRecorded"
                    + " FROM      ComponentStatus AS ComponentStatus_1"
                    + " WHERE   (component_id in (select component_id From Components WHERE machine_id='" + machineID + "'))))";
        CreateConn objcon = new CreateConn();
        DataTable dt = objcon.SQLConnDataTable(query);
        foreach (DataRow dr in dt.Rows)
        {
            string statusID = dr["status_id"].ToString();
            src = StatusToIcon(statusID);
        }
        return src;
    }

    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e) {
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
    }

    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        Server.Transfer("OilModuleMain.aspx");
    }
}
